//
//  ShareCoreData.m
//  吃货·上海
//
//  Created by lanou on 15/12/1.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "ShareCoreData.h"

@implementation ShareCoreData

+ (ShareCoreData *)shareInstance
{
    static ShareCoreData *CoreData = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (CoreData == nil) {
            CoreData = [[ShareCoreData alloc]init];
        }
    });
    return CoreData;
}



- (void)createCoreDat
{
    self.context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"CoreData" ofType:@"momd"];
    NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]];
    NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
    
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *sqlite = [doc stringByAppendingPathComponent:@"CoreData.sqlite"];
    
    NSLog(@"%@",sqlite);
    
    [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlite] options:nil error:nil];
    
    self.context.persistentStoreCoordinator = store;
    
}

#pragma mark - add Data
- (void)insertData:(SHMenuModel *)model
{
    if (_context == nil) {
        [self createCoreDat];
    }else if(model.makingmaterial != nil)
    {
        MenuModel *dataModel = [NSEntityDescription insertNewObjectForEntityForName:@"MenuModel" inManagedObjectContext:self.context];
        dataModel.title = model.title;
        dataModel.imageUrl = model.imageUrl;
        dataModel.info =  model.info;
        dataModel.myClass = model.myClass;
        dataModel.makingmaterial = model.makingmaterial;
        dataModel.makingMethod = model.makingMethod;
        dataModel.mainmatcrials = model.mainmatcrials;
        
        [self.context save:nil];
    }
}

#pragma mark - delete Data
- (void)deleteDataWithTitle:(NSString *)title
{
    if (_context == nil) {
        [self createCoreDat];
    }
    NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"MenuModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@",title];
    request.predicate = predicate;
    
    MenuModel *model = [self.context executeFetchRequest:request error:nil].lastObject;
    
    [self.context deleteObject:model];
    
    [self.context save:nil];
}


#pragma mark - select AllData
- (NSArray *)selectAllDataWithSortString:(NSString *)sortString
{
    if (_context == nil) {
        [self createCoreDat];
    }
    NSFetchRequest *request  =[NSFetchRequest fetchRequestWithEntityName:@"MenuModel"];
    
    if (sortString != nil) {
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:[NSString stringWithFormat:@"%@",sortString] ascending:YES];
        request.sortDescriptors = @[sort];
    }
    
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    return array;
}

#pragma mark - select oneData
- (MenuModel *)selectOneDataWithTitle:(NSString *)titleString
{
    if (_context == nil) {
        [self createCoreDat];
    }
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MenuModel"];
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"title = %@",titleString];
    request.predicate = predicate;
    
    MenuModel *model = [self.context executeFetchRequest:request error:nil].lastObject;
    [self.context save:nil];
    
    return model;
}

@end
